package cn.itrip.search.service;

import cn.itrip.vo.ItripHotelSearchAllInfo;
import cn.itrip.pojo.ItripHotelSearchInfo;
import cn.itrip.vo.hotel.SearchHotCityVO;
import cn.itrip.vo.hotel.SearchHotelVO;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;

/**
 * Created by LENOVO on 2018/8/23.
 */
@Service
public class HotelSearchServiceImpl implements HotelSearchService {
    /**
     * solr数据路径
     */
    public static String url = "http://localhost:8081/solr/hotel/";
    /**
     * 初始化httpSolrClient
     */
    HttpSolrClient httpSolrClient = new HttpSolrClient(url);
    /**
     * 查询数量
     */
    private int total=0;
    /***
     * 查询酒店列表
     * @param searchHotelVO
     * @return
     * @throws Exception
     */
    @Override
    public ItripHotelSearchAllInfo searchItripHotelPage(SearchHotelVO searchHotelVO) throws Exception {
        httpSolrClient.setParser(new XMLResponseParser());
        httpSolrClient.setConnectionTimeout(500);

        if(searchHotelVO.getPageNo()==null)
        {
            searchHotelVO.setPageNo(1);
        }
        searchHotelVO.setPageSize(10);
        List<ItripHotelSearchInfo> list=this.search(searchHotelVO);
        ItripHotelSearchAllInfo itripHotelSearchAllInfo = new ItripHotelSearchAllInfo();
        itripHotelSearchAllInfo.setBeginPos((searchHotelVO.getPageNo()-1)*searchHotelVO.getPageSize());
        itripHotelSearchAllInfo.setCurPage(searchHotelVO.getPageNo());
        itripHotelSearchAllInfo.setPageCount((total + searchHotelVO.getPageSize() - 1) /searchHotelVO.getPageSize());
        itripHotelSearchAllInfo.setPageSize(searchHotelVO.getPageSize());
        itripHotelSearchAllInfo.setTotal(total);
        for(int count=0;count<list.size();count++)
        {
            if(list.get(count).getAvgScore()==null)
            {
                list.get(count).setAvgScore(5.0);
            }
            if(list.get(count).getCommentCount()==null)
            {
                list.get(count).setCommentCount(0);
            }
            if(list.get(count).getIsOkCount()==null)
            {
                list.get(count).setIsOkCount(0);
            }
        }
        itripHotelSearchAllInfo.setRows(list);
        return itripHotelSearchAllInfo;
   }

    /**
     * 进行搜索酒店
     * @param searchHotelVO
     * @return
     */
   private List<ItripHotelSearchInfo> search(SearchHotelVO searchHotelVO) throws IOException, SolrServerException {

       SolrQuery query = new SolrQuery("*:*");
       if(searchHotelVO!=null)
       {
           StringBuilder sb=new StringBuilder();
           if(searchHotelVO.getDestination()!=null&&searchHotelVO.getDestination().length()!=0)
           {
               sb.append("destination:"+searchHotelVO.getDestination());
               if(searchHotelVO.getKeywords()!=null&&searchHotelVO.getKeywords().length()!=0)
               {
                   String keyWord=new String(searchHotelVO.getKeywords().getBytes("ISO-8859-1"),"UTF-8");
                   sb.append(" AND keyword:"+keyWord);
               }
           }
           else if(searchHotelVO.getKeywords()!=null&&searchHotelVO.getKeywords().length()!=0)
           {
               sb.append("keyword:"+searchHotelVO.getKeywords());
           }
           if(sb.toString()!=null&&sb.toString().length()!=0)
           {
               query.setQuery(sb.toString());
           }
           if(searchHotelVO.getHotelLevel()!=null&&searchHotelVO.getHotelLevel()!=0)
           {
               query.addFilterQuery("hotelLevel:"+searchHotelVO.getHotelLevel());
           }
           if (searchHotelVO.getFeatureIds()!=null&&searchHotelVO.getFeatureIds().length()!=0) {
               String []featureIds=searchHotelVO.getFeatureIds().split(",");
               StringBuilder featureIdBuff = new StringBuilder("(");
               for(int count=0;count<featureIds.length;count++)
               {
                   if(count==0)
                   {
                       featureIdBuff.append(" featureIds:" + "*," + featureIds[0] + ",*");
                   }
                   else
                   {
                       featureIdBuff.append(" OR featureIds:" + "*," + featureIds[count] + ",*");
                   }
               }
               featureIdBuff.append(")");
               query.addFilterQuery(featureIdBuff.toString());
           }
           if (searchHotelVO.getTradeAreaIds()!=null&&searchHotelVO.getTradeAreaIds().length()!=0) {
               StringBuilder tradeAreaIdBuff = new StringBuilder("(");
               String []tradeAreaId = searchHotelVO.getTradeAreaIds().split(",");
               for(int count=0;count<tradeAreaId.length;count++)
               {
                   if(count==0)
                   {
                       tradeAreaIdBuff.append(" tradingAreaIds:" + "*," + tradeAreaId[0] + ",*");
                   }
                   else
                   {
                       tradeAreaIdBuff.append(" OR tradingAreaIds:" + "*," + tradeAreaId[count] + ",*");
                   }
               }
               tradeAreaIdBuff.append(")");
               query.addFilterQuery(tradeAreaIdBuff.toString());
           }
           if (searchHotelVO.getMinPrice()!=null&&searchHotelVO.getMaxPrice()==null) {
               query.addFilterQuery("minPrice:" + "["+searchHotelVO.getMinPrice()+" TO *]");
           }
           if (searchHotelVO.getMinPrice()!=null&&searchHotelVO.getMaxPrice()!=null) {
               query.addFilterQuery("minPrice:" + "["+searchHotelVO.getMinPrice()+" TO "+ searchHotelVO.getMaxPrice()+"]");
           }
           if (searchHotelVO.getAscSort()!= null &&searchHotelVO.getAscSort().length()!=0) {
               query.addSort(searchHotelVO.getAscSort(), SolrQuery.ORDER.asc);
           }
           if (searchHotelVO.getDescSort()!= null && searchHotelVO.getDescSort().length()!=0) {
               query.addSort(searchHotelVO.getDescSort(), SolrQuery.ORDER.desc);
           }
           query.setStart((searchHotelVO.getPageNo()-1)*searchHotelVO.getPageSize());
           query.setRows(searchHotelVO.getPageSize());
       }
       QueryResponse queryResponse=httpSolrClient.query(query);
       SolrDocumentList docs = queryResponse.getResults();
       total=new Long(docs.getNumFound()).intValue();
       return queryResponse.getBeans(ItripHotelSearchInfo.class);
   }

    /**
     * 根据热门城市id查询酒店列表
     * @param searchHotCityVO
     * @return
     * @throws Exception
     */
    @Override
    public ItripHotelSearchAllInfo searchItripHotelByHotCity(SearchHotCityVO searchHotCityVO) throws Exception {
        httpSolrClient.setParser(new XMLResponseParser());
        httpSolrClient.setConnectionTimeout(500);
        List<ItripHotelSearchInfo> list=this.searchByHotCity(searchHotCityVO);
        ItripHotelSearchAllInfo itripHotelSearchAllInfo = new ItripHotelSearchAllInfo();
        itripHotelSearchAllInfo.setRows(list);
        return itripHotelSearchAllInfo;
    }

    /**
     * 进行搜索查询酒店根据热门城市id
     * @param searchHotCityVO
     * @return
     */
    private List<ItripHotelSearchInfo> searchByHotCity(SearchHotCityVO searchHotCityVO) throws IOException, SolrServerException {
        SolrQuery query = new SolrQuery("*:*");
        if(searchHotCityVO!=null)
        {
           if (searchHotCityVO.getCityId()!=null) {
                query.addFilterQuery("cityId:" + searchHotCityVO.getCityId());
           }
            if(searchHotCityVO.getCount()!=null)
            {
                query.setStart(0);
                query.setRows(searchHotCityVO.getCount());
            }

        }
        QueryResponse queryResponse = httpSolrClient.query(query);
        return queryResponse.getBeans(ItripHotelSearchInfo.class);
    }
}
